<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Db;

class Index extends Controller
{
    public function _initialize()
    {
        if(!Session::has('Adminname')){
            $this->error('请登录!','../admin');
        }
    }
    public function index()
    {
    	$list = Db::name('news')->order('id','asc')->paginate(10);
    	$page = $list->render();
    	$this->assign('list', $list);
		$this->assign('page', $page);
        return $this->fetch('index');
    }

    //删除新闻
    public function delete()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data = $news -> deletenews($param);
    	if($data){
    		return json(['message'=>'删除成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }

    //更新页面
    public function updatedetail()
    {
        $news = model('News');
        $param = request()->get('id');
        $data = $news::get($param);
        $this->assign('list',$data);
        return $this->fetch('detail/news');
    }

    //取消发布
    public function cancel()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data=$news->cancelnews($param);
    	if($data){
    		return json(['message'=>'取消发布成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }

    //发布
    public function fabu()
    {
    	$news = model('News');
    	$param = request()->get('id');
    	$data=$news->fabunews($param);
    	if($data){
    		return json(['message'=>'发布成功!']);
    	}
    	return json(['message'=>$news->getError()]);
    }
}
